<template>
  <div class="p-4">
    <el-card class="box-card">
      <div slot="header" class="clearfix flex justify-between">
        <el-form inline class="flex-grow">
          <el-form-item>
            <el-button
              type="success"
              icon="el-icon-plus"
              @click="deBounceWrap(handleAdd)"
              >新增</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="deBounceWrap(handleDelAll)"
              >批量删除</el-button
            >
          </el-form-item>
        </el-form>

        <div class="flex-grow-0">
          <el-select
            v-model="listQuery.status"
            placeholder="请选择开启状态"
            clearable
          >
            <el-option label="开启" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
          <el-input
            class="ml-4"
            style="width: 35%"
            v-model="listQuery.tag_name"
            placeholder="请输入标签名称"
            clearable
          ></el-input>
          <el-button
            @click="getList"
            type="primary"
            class="el-icon-search p-1 text-white text-2xl bg-green-500 cursor-pointer"
            >搜索</el-button
          >
        </div>
      </div>

      <el-table :data="data" @selection-change="handleSelectionChange" border>
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="序号"></el-table-column>
        <el-table-column prop="name" label="标签名称"></el-table-column>
        <el-table-column prop="bg_color" label="背景颜色"></el-table-column>
        <el-table-column prop="icon" label="标签图片"></el-table-column>
        <el-table-column prop="status" label="套装状态">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.status === 1">启用</el-tag>
            <el-tag type="danger" v-else>禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="添加时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              plain
              size="mini"
              icon="el-icon-edit"
              @click="deBounceWrap(handleEdit, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              plain
              size="mini"
              icon="el-icon-delete"
              @click="deBounceWrap(handleDel, scope.row)"
              >删除</el-button
            >
            <!-- <el-dropdown trigger="click" style="margin-left: 20px">
              <el-button type="primary"
                >操作<i class="el-icon-arrow-down el-icon--right"></i
              ></el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  >不启用</el-dropdown-item
                >
                <el-dropdown-item>定时任务</el-dropdown-item>
                <el-dropdown-item>置顶</el-dropdown-item>
                <el-dropdown-item
                  @click.native="deBounceWrap(handleDel, scope.row)"
                  >删除</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown> -->
          </template>
        </el-table-column>
      </el-table>
      <cus-pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      :title="titleOptions[title]"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="40%"
      @close="handleClear"
      :before-close="handleClose"
    >
      <el-form :model="form" ref="form" :rules="rules" label-width="120px">
        <el-form-item prop="tag_name" label="标签名称">
          <el-input
            placeholder="请输入标签名称"
            v-model="form.tag_name"
            clearable
            :maxlength="4"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item prop="bg_color" label="背景颜色">
          <el-input
            v-model="form.bg_color"
            placeholder="请输入背景颜色"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="标签图片" prop="icon">
          <el-upload
            :http-request="customizeUpload"
            :on-success="handleSuccess"
            :show-file-list="false"
            action=""
            class="avatar-uploader"
          >
            <img v-if="form.icon" :src="form.icon" class="avatar"/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item prop="status" label="状态">
          <el-radio v-model="form.status" :label="1">启用</el-radio>
          <el-radio v-model="form.status" :label="0">禁用</el-radio>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" plain @click="handleClose">返回</el-button>
        <el-button type="primary" @click="deBounceWrap(handleSumbit, 'form')"
          >提交</el-button
        >
      </div>
    </el-dialog>

    <!-- 删除弹框 -->
    <el-dialog
      title="确定删除吗?"
      :visible.sync="deleteDialogVisible"
      width="435px"
    >
      <span style=""
        ><em style="font-style: normal; color: red">删除后不可恢复</em
        >，你还要继续吗？</span
      >
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getListApi,
  addGiftList,
  editGiftList,
  delGiftList
} from "@/api/roomGift/labelManagement";
export default {
  data() {
    return {
      data: [],
      listQuery: {
        status: undefined,
        tag_name: undefined,
        page: 1,
        page_size: 10
      },
      total: 0,
      titleOptions: {
        add: "新增",
        edit: "编辑"
      },
      title: "add",
      rowData: {},
      form: {
        tag_name: undefined,
        bg_color: undefined,
        status: undefined,
        icon: undefined
      },
      rules: {
        tag_name: [
          { required: true, message: "请输入标签名称", trigger: "blur" }
        ],
        bg_color: [
          { required: true, message: "请选择背景颜色", trigger: "change" }
        ],
        status: [
          { required: true, message: "请选择开启状态", trigger: "change" }
        ],
        icon: [
          { required: true, message: "请上传标签图片", trigger: "change" }
        ]
      },
      dialogVisible: false,
      //删除
      deleteDialogVisible: false,
      //勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: "",
      currentId: ""
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //列表
    getList() {
      let data = JSON.parse(JSON.stringify(this.listQuery));
      if (!this.listQuery.status) {
        data.status = -1;
      }

      getListApi(data).then(res => {
        this.data = res.data.data;
        this.total = res.data.total;
      });
    },

    //新增
    handleAdd() {
      this.title = "add";
      this.dialogVisible = true;
    },

    // 编辑
    handleEdit(row) {
      this.title = "edit";
      this.rowData = row;
;
      this.form.status = this.rowData.status;
      this.form.tag_name = this.rowData.name;
      this.form.bg_color = this.rowData.bg_color;
      this.form.icon = this.rowData.icon;
      this.dialogVisible = true;
    },
    handleSumbit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.title === "add") {
            let data = {
              bg_color: this.form.bg_color,
              tag_name: this.form.tag_name,
              status: this.form.status,
              icon: this.form.icon
            };

            addGiftList(data).then(res => {
              if (res.code === 200) {
                this.$message.success("提交成功");
                this.getList();
                this.dialogVisible = false;
              } else {
                this.$message.error(res.message)
              }
            });
          }
          if (this.title === "edit") {
            let data1 = {
              id: this.rowData.id,
              bg_color: this.form.bg_color,
              tag_name: this.form.tag_name,
              status: this.form.status,
              icon: this.form.icon
            };

            editGiftList(data1).then(res => {
              if (res.code === 200) {
                this.$message.success("编辑成功");
                this.getList();
                this.dialogVisible = false;
              }else {
                this.$message.error(res.message)
              }
            });
          }
        } else {

          return false;
        }
      });
    },
    //清空表单
    handleClose() {
      this.dialogVisible = false;
    },
    handleClear() {
      this.$refs["form"].resetFields();
      this.form = {
        tag_name: undefined,
        bg_color: undefined,
        status: undefined,
        icon: undefined
      };
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleDelAll() {
      if (this.multipleSelection.length) {
        /* 存在勾选数据 */ const ids = this.multipleSelection.reduce(
          (pre, currRow) => {
            pre += `${currRow.id},`;
            return pre;
          },
          ""
        );
        delGiftList({ ids })
          .then(() => this.$message.success("删除成功！"))
          .catch(console.error)
          .finally(() => /* 刷新数据 */ {
            this.multipleSelection = [];
            this.getList();
          });
      } /* 不存在勾选数据 */ else {
        this.$message.warning("请勾选要删除的数据！");
      }
    },
    // 删除
    handleDel(row) {
      this.dataIds = { ids: `${row.id}` };
      this.deleteDialogVisible = true;
    },
    // 确定删除
    confirmDelete() {
      delGiftList(this.dataIds).then(response => {
        if (response.code === 200) {
          this.getList();
          this.deleteDialogVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功"
          });
        }
      });
    },
    handleSuccess() {
      this.form.icon = this.ossLastImgUrl
    },
  }
};
</script>
<style lang="scss" scoped>
::v-deep.avatar-uploader .el-upload {
  border        : 1px dashed #d9d9d9;
  border-radius : 6px;
  cursor        : pointer;
  position      : relative;
  overflow      : hidden;
}

.avatar-uploader .el-upload:hover {
  border-color : #409eff;
}

.avatar-uploader-icon {
  font-size   : 28px;
  color       : #8c939d;
  width       : 100px;
  height      : 100px;
  line-height : 100px;
  text-align  : center;
}

.avatar {
  width   : 100px;
  height  : 100px;
  display : block;
}</style>
